<template>
  <div class="rights">
    <!-- 权限管理 -->
    <el-form :inline="true"
             :model="dataForm"
             @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.roleName"
                  style="width: 330px"
                  placeholder="角色名称"
                  clearable></el-input>
      </el-form-item>
      <el-form-item>
        <!-- 搜素按钮 -->
        <el-button type="primary"
                   icon="el-icon-search"
                   @click="getDataList()"></el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   v-if="isAuth('sys:role:save')"
                   @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="info"
                   v-if="isAuth('sys:role:delete')"
                   @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0"
                   class="btnColor">批量删除</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="dataList"
              border
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%;">
      <el-table-column type="selection"
                       header-align="center"
                       align="center"
                       width="50">
      </el-table-column>
      <el-table-column prop="roleId"
                       fixed
                       header-align="center"
                       align="center"
                       width="72"
                       label="ID">
      </el-table-column>
      <el-table-column prop="roleName"
                       header-align="center"
                       align="center"
                       width="206"
                       label="角色">
      </el-table-column>
      <el-table-column prop="remark"
                       header-align="center"
                       align="center"
                       width="557"
                       label="备注">
      </el-table-column>
      <el-table-column prop="createTime"
                       header-align="center"
                       align="center"
                       width="160"
                       label="创建时间">
      </el-table-column>
      <el-table-column fixed="right"
                       header-align="center"
                       align="center"
                       width="486"
                       label="操作">
        <template slot-scope="scope">
          <el-button @click="addOrUpdateHandle(scope.row.roleId)"
                     v-if="isAuth('sys:role:update')"
                     type="text"
                     size="small">修改</el-button>
          <el-button type="text"
                     v-if="isAuth('sys:role:delete')"
                     size="small"
                     @click="deleteHandle(scope.row.roleId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <!-- 分页前说明 -->
      <div class="instructions">显示第1到第{{dataList.length}}条数据，总共{{totalPage}}条数据
        <!-- ，每页显示 -->
        <!-- 条数据 -->
      </div>
      <!-- 分页 -->
      <el-pagination @size-change="sizeChangeHandle"
                     @current-change="currentChangeHandle"
                     :current-page="pageIndex"
                     :page-sizes="[10, 20, 50]"
                     :page-size="pageSize"
                     :total="totalPage"
                     layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 新增 -->
      <addRights v-if="addOrUpdateVisible"
                 ref="addOrUpdate"
                 @refreshDataList="getDataList"></addRights>
    </div>
  </div>
</template>

<script>
import addRights from './add/addRights'
export default {
  data () {
    return {
      dataForm: {
        roleName: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    addRights
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sys/role/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'roleName': this.dataForm.roleName
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.roleId
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/role/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => { })
    }
  }
}
</script>

<style lang="scss">
.rights {
  .pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .instructions {
      height: 9px;
      font-size: 14px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      line-height: 19px;
      color: #333333;
    }
  }
  .license_img {
    width: 70px;
    height: 50px;
  }
  // 导入
  button.el-button.btnColor.el-button--info.el-button--medium {
    position: relative;
    overflow: hidden;
  }
  .file {
    display: inline;
    position: relative;
    overflow: hidden;
    .fileBtn {
      opacity: 0;
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }
  }
}
</style>